<?php
namespace api\modules\merchant\controllers\goods;

use backend\widgets\provinces\Provinces;
use common\models\store\Store;
use Yii;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;


/**
 * 商品分类接口
 *
 * Class InfoController
 * @package api\modules\merchant\controllers\goods
 * @property \yii\db\ActiveRecord $modelClass
 * @author tjj
 */
class GclassController extends OnAuthController
{
    public $modelClass = 'common\models\goods\GClass';


    /**
     * 批量更新排序
     * @return int
     */
    public function actionResort(){
        $data = Yii::$app->request->post('data');

        $data = is_string($data)?json_decode($data,true):$data;

        $num = 0;
        foreach ($data as $item){
            $id = $item['id']??0;
            $sort = $item['sort']??0;

            if($id){
                $model = $this->modelClass::findOne(['id'=>$id]);
                if($model){
                    $model->sort = $sort;
                    $model->save(false);
                    $num ++;
                }
            }
        }

        return $num;
    }
    /**
     * 详情
     * @param $id
     * @return array|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionView($id)
    {
        //return parent::actionView($id); // TODO: Change the autogenerated stub

        $model = $this->modelClass::find()
            ->select(['id','store_id','name','parent_id','sort','status'])
            ->where(['id'=>$id])
            ->one();

        $this->checkStore($model->store_id);

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 列表
     * @return array|mixed|\yii\data\ActiveDataProvider
     */
    public function actionIndex()
    {
        //parent::actionIndex(); // TODO: Change the autogenerated stub

        $store_id = Yii::$app->request->get('store_id');

        $this->checkStore($store_id);

        $list = $this->modelClass::find()
            ->select(['id','store_id','name','parent_id','sort','status'])
            ->where(['store_id'=>$store_id,'status' => StatusEnum::ENABLED])
            ->asArray()
            ->all();

        return $list;
    }

    /**
     * 创建分组
     * @return mixed|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionCreate()
    {
        $store_id = Yii::$app->request->post('store_id')??0;

        if(!$store_id){
            throw new NotFoundHttpException('店铺参数有误.');
        }

        $this->checkStore($store_id);

        $res = parent::actionCreate(); // TODO: Change the autogenerated stub

        $model = $this->modelClass::find()
            ->select(['id','store_id','name','parent_id','sort','status'])
            ->where(['id'=>$res->id])
            ->one();

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 修改信息
     * @param $id
     * @return array|mixed|\yii\db\ActiveRecord|null
     * @throws NotFoundHttpException
     */
    public function actionUpdate($id)
    {
        $model = $this->modelClass::findOne(['id'=>$id]);
        if($model){
            $this->checkStore($model['store_id']);
        }else return ResultDataHelper::api(422, '请求的数据不存在');

        parent::actionUpdate($id); // TODO: Change the autogenerated stub

        $model = $this->modelClass::find()
            ->select(['id','store_id','name','parent_id','sort','status'])
            ->where(['id'=>$id])
            ->one();

        if (!$model)
        {
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');
        }

        return $model;
    }

    /**
     * 删除
     * @param $id
     * @return bool|mixed
     * @throws NotFoundHttpException
     */
    public function actionDelete($id)
    {
        $model = $this->modelClass::findOne(['id'=>$id]);
        if($model){
            $this->checkStore($model['store_id']);
        }else return ResultDataHelper::api(422, '请求的数据不存在');

        return parent::actionDelete($id); // TODO: Change the autogenerated stub
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
//        if (in_array($action, ['delete', 'index']))
//        {
//            throw new \yii\web\BadRequestHttpException('权限不足');
//        }
    }
}
